package Pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        ThreadFactory daemonThreadFactory = r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true); // 标记为守护线程
            thread.setName("Daemon-Thread");
            return thread;
        };
        //ExecutorService executorService = Executors.newCachedThreadPool(daemonThreadFactory);//创建一个普通线程池

        ExecutorService executorService = Executors.newFixedThreadPool(10);//创建一个固定线程数的线程池
        for (int i = 0; i < 10; i++) {
            final int id = i;
            //提交任务
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "-" + id);//匿名内部类访问外部成员，变量捕获
                }
            });
        }
        //Thread.sleep(1000);
        //executorService.shutdown();
    }
}

